package cn.mumzone.pos.presenter;

import android.text.TextUtils;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.PatternUtils;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.presenter.contract.BabyAddOrModifyContract;

public class BabyAddOrModifyPresenter extends AbsPresenter<BabyAddOrModifyContract.View, MemberRepository> implements BabyAddOrModifyContract.Presenter {
    @Override
    public void createNewBaby(String babyName, String babyBirth, String babyHeight, String babyWeight,
                              String selectGender, final String babyId, String memberId, String labour) {

        ApiCallBack<MemberEntity.BabysBean> callback = new ApiCallBack<MemberEntity.BabysBean>() {

            @Override
            public void onSuccess(MemberEntity.BabysBean baby) {
                if (TextUtils.isEmpty(babyId)) {
                    mView.onCreateOrModifyBabyInfoSuccess(baby, true);
                } else {
                    mView.onCreateOrModifyBabyInfoSuccess(baby, false);
                }

            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
                if (TextUtils.isEmpty(babyId)) {
                    mView.onCreateOrModifyBabyInfoFailed(true);
                } else {
                    mView.onCreateOrModifyBabyInfoFailed(false);
                }
            }
        };
        mRepo.createNewBaby(babyName, babyBirth, babyHeight, babyWeight, selectGender, babyId, memberId, labour,
                callback);
        addSubscrebe(callback);
    }

    @Override
    public boolean isInputLegal(String babyName, String babyBirthday) {
        if (TextUtils.isEmpty(babyName)) {
            mView.showError("请输入宝宝姓名");
            return false;
        }

        if (!PatternUtils.checkName(babyName)) {
            mView.showError("宝宝姓名不能输入特殊字符");
            return false;
        }

        if (TextUtils.isEmpty(babyBirthday)) {
            mView.showError("请选择宝宝生日");
            return false;
        }

        return true;
    }
}
